
const db = wx.cloud.database()
const _ = db.command
const $ = db.command.aggregate
const app = getApp()

import store from '../store/index'
var util = require('../utils/util.js')
const _openid = store.data.openid


const getTempFile = async (params)=>{
  var fileList = params
  console.log(fileList)
  return new Promise((reslove,reject)=>{
    wx.cloud.callFunction({
      name:"file",
      data:{action:"getTempFile",data:{fileList}}
    }).then(res=>{     
      reslove(res)  
    })
  })
}


const downloadResume = async (params)=>{
  const fileList = params.fileList
  return new Promise((reslove,reject)=>{
   
    getTempFile(fileList).then(res=>{
      console.log(res)
      var url  = res.result[0].tempFileURL
      console.log(url)
       //下载文件，生成临时地址
      wx.downloadFile({
        url: url,
        success(res) {
         wx.saveFile({
            tempFilePath: res.tempFilePath,
            success: function (res) {
              const savedFilePath = res.savedFilePath;
              // 打开文件
              wx.openDocument({
                filePath: savedFilePath,
                success: function (res) {
                  wx.hideLoading
                  reslove({code:200,massage:"打开文件成功",data:res})
                },
              });
            },
            fail: function (err) {
             reject({code:300,massage:"打开文件失败",data:err})
            }
          });
        },
        fail(res) {
          console.log("生成临时文件地址失败")
        }
      })
    })
    
  })
  
} 


const uploadResume = async(params)=>{
  return new Promise((reslove,reject)=>{
    wx.chooseMessageFile({
      count: 1,
      type: 'file',
      success (res) {
        console.log(res);
        var file = res.tempFiles[0]
        const name = file.name;
        const time = app.formatDate(new Date());
        var arr = file.name.split('.');
        var suffix = arr.length > 1 ? '.' + arr[arr.length-1] : ''
        wx.showLoading({title: '上传中'})
        wx.cloud.uploadFile({
          cloudPath: "resume/"+_openid + suffix,
          filePath: file.path,
          success: res => {
            
            db.collection('user').where({
              _openid:_openid
            }).update({
              data:{
                  resume:_.push({file:res.fileID,name,time,})
              },
            }).then(res=>{
              reslove({...res,file})
            })
          },
          complete: wx.hideLoading
        })
      }
    })


  })
  
}

const uploadWorks = async(params)=>{
  return new Promise((reslove,reject)=>{
    wx.chooseMessageFile({
      count: 1,
      type: 'file',
      success (res) {
        console.log(res)
        var file = res.tempFiles[0]
        const name = file.name
        const time = app.formatDate(new Date())
        var arr = file.name.split('.');
        var suffix = arr.length > 1 ? '.' + arr[arr.length-1] : ''
        wx.showLoading({title: '上传中'})
        wx.cloud.uploadFile({
          cloudPath: "works/"+_openid + suffix,
          filePath: file.path,
          success: res => {
            
            db.collection('user').where({
              _openid:_openid
            }).update({
              data:{
                  works:_.push({file:res.fileID,name,time,})
              },
            }).then(res=>{
              reslove({...res,file})
            })
          },
          complete: wx.hideLoading
        })
      }
    })


  })
  
}
const send = async (params)=>{
  var {file,targetID} = params
  var Myopenid = store.data.openid
  var action = params.action
  console.log(Myopenid)
  return new Promise((reslove,reject)=>{
    wx.cloud.callFunction({
      name:"file",
      data:{action:action,data:{file,targetID,Myopenid}}
    }).then(res=>{
        reslove(res)
    }).catch(err=>{
      reject(err)
    })
  })
  
}
const del = async (params)=>{
  var file = params.file
  console.log(file)
  return new Promise((resolve,reject)=>{
    console.log(app.globalData.openid)
    db.collection('user').where({
      _openid:app.globalData.openid
    }).update({
      data:{
        resume:_.pull({file:file})
      }
      
    }).then(res=>{
      resolve(res)
    }).catch(err=>{
      reject(err)
    })
  
  })
 
}
const del2 = async (params)=>{
  var file = params.file
  console.log(file)
  return new Promise((resolve,reject)=>{
    console.log(app.globalData.openid)
    db.collection('user').where({
      _openid:app.globalData.openid
    }).update({
      data:{
        works:_.pull({file:file})
      }
      
    }).then(res=>{
      resolve(res)
    }).catch(err=>{
      reject(err)
    })
  
  })
 
}



export default{
  downloadResume,
  uploadResume,
  send,
  uploadWorks,
  del,
  del2

  
}